function NutSystem() {
	this.item = function(x, i) {
		e = document.getElementById("nut" + x.trim());
		if (!e)
			return '';
		if (i != undefined)
			e = e.children[i];
		return e.innerHTML;
	};
	this.nutDetail = function(b) {
		nutItem = document.getElementById(b);
		dialog = document.getElementById("dialog");
		list = nutItem.children;
		h = list[1].innerHTML + "<hr/>" + list[2].innerHTML;
		dialog.innerHTML = h;
		$('#dialog').dialog('open');
		return false;
	};
	this.trigger = function(x) {
		p = x.parentNode;
		c = p.children[1];
		style = c.style;
		style.display = style.display == 'block' ? 'none' : 'block';
		return false;
	};
	this.tableRow = function(col) {
		var h = new String();
		h += "<tr>";
		var i;
		for (i = 0; i < col.length; i++)
			h += "<td " + col[i].attrib + ">" + col[i].text + "</td>";
		h += "</tr>";
		return h;
	};
	this.tableHead = function(col) {
		var b = new String();
		b += "<thead>";
		b += "<tr>";
		for (i = 0; i < col.length; i++) {
			col[i] = new String(col[i]);
			col[i].attrib = col[i].attrib == undefined ? "" : col[i].attrib;
			b += "<th " + col[i].attrib + ">" + col[i] + "</th>";
		}
		b += "</tr>";
		b += "</thead>";
		return b;
	};
	this.farmers = function() {
		col = [
			'Name'
		];
		c = 0;
		var h = new String();
		h += "<div class='NutFarmerTable'>";
		h += "<a onclick='return nutSystem.trigger(this)' href=''>Team</a>";
		h += "<div style='display: none;'>";
		h += "<table id='nutFarmerTable' class='tablesorter'>";
		h += this.tableHead(col);
		h += "<tbody>";
		list = getElementsByClassName('NutFarmer');
		var i;
		for (i = 0; i < list.length; i++) {
			item = list[i];
			c = 0;
			name = item.innerHTML;
			c = 0;
			col[c++].text = name;
			h += this.tableRow(col);
		}
		h += "</tbody>";
		h += "</table>";
		h += "</div>";
		h += "</div>";
		return h;
	};
	this.bags = function() {
		col = [
		        'Name', 'Desc'
		];
		var h = new String();
		h += "<div class='NutBagTable'>";
		h += "<a onclick='return nutSystem.trigger(this)' href=''>Schedule</a>";
		h += "<div style='display: none;'>";
		h += "<table id='nutBagTable' class='tablesorter'>";
		h += this.tableHead(col);
		list = getElementsByClassName('NutBag');
		for (i = 0; i < list.length; i++) {
			item = list[i];
			c = 0;
			col[c].text = item.children[c++].innerHTML;
			col[c].text = item.children[c++].innerHTML;
			h += this.tableRow(col);
		}
		h += "</table>";
		h += "</div>";
		h += "</div>";
		return h;
	};
	this.start = function() {
		var h = new String("<div id='dialog' class='NutDialog'></div>");
		h += this.farmers();
		h += this.bags();
		h += this.nuts();
		document.getElementById("nutBody").innerHTML = h;
		$(document).ready(function() {
			$("#nutTable").tablesorter();
			$("#nutFarmerTable").tablesorter();
			$("#nutBagTable").tablesorter();
			$("#nutRootTable").tablesorter();
			$("#dialog").dialog( {
				autoOpen : false
			});
		});
	};
	this.commaListHtml = function(x, offset) {
		list = x.children;
		var h = new String("");
		for (j = 0; j < list.length; j++)
			h += '<b class="NutCommaList">' + this.item(list[j].innerHTML,
			        offset).trim() + '</b>';
		return h;
	};
	this.nuts = function() {
		col = [
		        'Nut', 'Schedule', 'Desc', 'Category', 'Author', 'Related'
		];
		var h = new String();
		h += "<div class='NutTable'>";
		h += "<a href='#' onclick='return nutSystem.trigger(this)'>Development</a>";
		h += "<div style='display: none;'>";
		h += "<table id='nutTable' class='tablesorter'>";
		h += this.tableHead(col);
		h += "<tbody>";
		nuts = getElementsByClassName('Nut');
		for (n = 0; n < nuts.length; n++)
	        try {
				nut = nuts[n];
				i = 1;
				name = nut.children[i++].innerHTML;
				desc = nut.children[i++].children[0].children[1].children;
				desc = desc.length == 1 ? desc[0].innerHTML : desc[0].innerHTML
				        + "<a href='#' onclick='return nutSystem.nutDetail(\""
				        + nut.id + "\")'>...</a>";
				categories = this.commaListHtml(nut.children[i++]);
				authors = this.commaListHtml(nut.children[i++]);
				related = this.commaListHtml(nut.children[i++], 0);
				bag = this.item(nut.children[i++].innerHTML, 0);
				i = 0;
				col[i++].text = name;
				col[i++].text = bag;
				col[i++].text = desc;
				col[i++].text = categories;
				col[i++].text = authors;
				col[i++].text = related;
				h += this.tableRow(col);
			} catch (e) {
			}
		h += "</tbody>";
		h += "</table>";
		h += "</div>";
		h += "</div>";
		return h;
	};
	return this;
}
var nutSystem = new NutSystem();
$(document).ready(function() {
	nutSystem.start();
});
